from PIL import Image
import os
# http://c.biancheng.net/pillow/
"""
路径命令
os.path.abspath
os.path.dirname
os.path.join
"""
img_dir = os.path.dirname(os.path.abspath(__file__))
file_dir = os.path.join(img_dir,"img","1.png")

# Image 类是 Pillow 库中最为重要的类，该类被定义在和与其同名的 Image 模块中。
# open 打开图片
im = Image.open(file_dir,mode="r")
print(im,im.size,im.width,im.height,im.info,im.format)
# im.show()
im = im.convert('RGB')  # convert方法改变图片模式 -- 图片格式
im.save(os.path.join(img_dir,"img_out","1.jpg"),format=None)

# 新建一个图片 
# resize 修改尺寸
im_new = Image.new(mode='RGB',size=(100,100),color='#000f00').resize(size=(500,500))
print("size:",im_new.size)

#批量修改
# file = os.listdir(os.path.join(img_dir,"img"))
# print(file)

# for img in file:
#     # print(img)
#     im = Image.open(os.path.join(img_dir,"img",img)).resize(size=(500,500))
#     im.save(os.path.join(img_dir,"img_out",img))
#     print(im.size)

#颜色分离
base_dir = os.path.dirname(os.path.abspath(__file__))
imge_dir = os.path.join(img_dir,"img")

im = Image.open(imge_dir+"\\3.jpg").resize(size=(500,500))
print(im)
r,g,b = im.split()
# r.show()
# g.show()
im_merge = Image.merge("RGB",(b,r,b))
box = (0,0,50,500)
im_crop = im_merge.crop(box)
im_crop.show()
